﻿@model Bricks.Site.Models.UserView

@{
    //ViewBag.Title = "用户管理";
    //Layout = "~/Views/Shared/_UserPage.cshtml";  //Embed嵌入页面不能再次引用布局，否则可能获取不到数据
}

@*实现对EasyUI的DataGird控件操作的JS代码*@
<script type="text/javascript">
    $(function () {
        //当页面首次刷新的时候执行的事件
        initTable();

        //$("#myTree").tree({
        //    lines: true, url: "/Account/GetOrgTree",
        //    onSelect: function (node) {
        //        $("#myDataGrid").datagrid("clearChecked");
        //        $("#myDataGrid").datagrid("load", { departmentid: node.id });
        //    }
        //});
    });
    //实现对DataGird控件的绑定操作

    function initTable(queryData) {
        $('#myDataGrid').datagrid({   //定位到Table标签，Table标签的ID是myDataGrid
            //   ActionInfo是控制器，GetData是控制器中获取用户数据的一个Action
            url: '/Account/GetDataList',   //指向后台的Action来获取当前用户的信息的Json格式的数据
            //title: '用户管理',  //标识
            //iconCls: 'icon-save',
            //width: 'auto',
            //height: 500,
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            collapsible: true,
            pagination: true,
            pageSize: 20,
            rownumbers: true,
            //sortName: 'ID',    //根据某个字段给easyUI排序
            //sortOrder: 'asc',
            remoteSort: false,
            idField: 'ID',
            checkOnSelect: true,
            selectOnCheck: true,
            singleSelect: true,
            queryParams: queryData,  //异步查询的参数
            columns: [[
                //{ field: 'ck', checkbox: true },   //选择
                //{ title: '@Html.LabelFor(m => m.ID)', field: 'ID', width: 265, sortable: true },
                { title: '@Html.LabelFor(m => m.UserName)', field: 'UserName', width: 80, sortable: true },
                { title: '@Html.LabelFor(m => m.RealName)', field: 'RealName', width: 80, sortable: true },
                { title: '@Html.LabelFor(m => m.Position)', field: 'Position', width: 80, sortable: true },
                { title: '@Html.LabelFor(m => m.Email)', field: 'Email', width: 140, sortable: true },
                { title: '@Html.LabelFor(m => m.Contactphone)', field: 'Contactphone', width: 120, sortable: true },
                { title: '@Html.LabelFor(m => m.OrgName)', field: 'OrgName', width: 110, sortable: true },
                {
                    title: '@Html.LabelFor(m => m.IsPurviewAdmin)', field: 'IsPurviewAdmin', width: 68, sortable: true, align: 'center',
                    formatter: function (value, row, index) {
                        if (value == true) {
                            return '<input type="checkbox" checked="true" onclick="return false;" />';
                        }
                        else {
                            return '<input type="checkbox" onclick="return false;" />';
                        }
                    }
                },
                {
                    title: '@Html.LabelFor(m => m.IsHandoutAdmin)', field: 'IsHandoutAdmin', width: 68, sortable: true, align: 'center',
                    formatter: function (value, row, index) {
                        if (value == true) {
                            return '<input type="checkbox" checked="true" onclick="return false;" />';
                        }
                        else {
                            return '<input type="checkbox" onclick="return false;" />';
                        }
                    }
                },
                {
                    title: '@Html.LabelFor(m => m.IsFlowApprovalPerson)', field: 'IsFlowApprovalPerson', width: 68, sortable: true, align: 'center',
                    formatter: function (value, row, index) {
                        if (value == true) {
                            return '<input type="checkbox" checked="true" onclick="return false;" />';
                        }
                        else {
                            return '<input type="checkbox" onclick="return false;" />';
                        }
                    }
                },
                { title: '@Html.LabelFor(m => m.Description)', field: 'Description', width: 150, sortable: true },
                {
                    title: '@Html.LabelFor(m => m.IsDeleted)', field: 'IsDeleted', width: 60, sortable: true, align: 'center',
                    formatter: function (value, row, index) {
                        return $.GetYesOrNo(value);
                    }
                },
                { title: '@Html.LabelFor(m => m.AddDate)', field: 'AddDate', width: 180, sortable: true, formatter: function (value, rec) { if (value) { return dateConvert(value); } } }
            ]],
            toolbar: '#myToolBar',

            //合并单元格
            onLoadSuccess: function (data) {
                if (data.rows.length > 1) {
                    //var tTable = $("#myDataGrid");
                    //mergeCellsGroupByField(tTable, "UserName,RealName,Email,Contactphone,Description,AddDate", 0, data.rows.length);
                    //以上合并方法是不是有问题呢？先只合并用户名
                    mergeCellsByField("myDataGrid", "UserName");
                }
            }
        });
    }

    var Account = {
        ////展开机构
        //ExpandTree: function () {
        //    $("#myTree").tree("expandAll");
        //},
        ////收起机构
        //ColloseTree: function () {
        //    $("#myTree").tree("collapseAll");
        //},
        ////查看全部
        //AllOrg: function () {
        //    var node = $("#myTree").tree("getRoot");
        //    $("#myTree").tree("select", node.target);
        //    $("#myDataGrid").datagrid("clearChecked");
        //    $("#myDataGrid").datagrid("load", { departmentid: node.id });
        //}
        //新增
        Add: function () {
            var orgRow = $("#myTreeGrid").treegrid("getSelected");
            if (orgRow) {
                Account.MutualAdd("新增", "/Account/Edit?newOrgID=" + orgRow["ID"]);
            }
            else {
                Account.MutualAdd("新增", "/Account/Edit");
            }
        },

        //实体操作（新增）
        MutualAdd: function (title, href) {
            Account.ModelDialogAdd({
                title: title, href: href, width: 360, maximizable: true,
                onLoad: function () { Account.FormInit(); }
            });
        },

        //实体窗口（新增）
        ModelDialogAdd: function (options) {
            if (options.dialogid) {
                options.dialogid = "id='" + options.dialogid + "'";
            }
            var buttons = [];
            if (!options.width) { options.width = "auto"; }
            if (!options.height) { options.height = "auto"; }

            buttons.push({
                text: "引入现有用户", iconCls: "icon-caution",
                handler: function () {
                    //$.ModelForm({
                    //    validate: true, button: $(this),
                    //    id: "#form-model",
                    //    url: "/Account/Edit",
                    //    success: function (result) {
                    //        Account.Reload();
                    //    }
                    //});
                    //$.EasyuiSlide("引入现有用户");
                    Account.MutualImport("引入现有用户", "/Account/ImportUser");
                }
            });
            buttons.push({
                text: "确定", iconCls: "icon-ok",
                handler: function () {
                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#form-model",
                        url: "/Account/Edit",
                        success: function (result) {
                            Account.Reload();
                        }
                    });
                }
            });
            buttons.push({ id: "btnClose", text: "关闭", iconCls: "icon-no", handler: function () { $.ButtonCloseDialog(this); } });

            var dialog = $("<div " + options.dialogid + " />").dialog({
                title: options.title, href: options.href,
                width: options.width, height: options.height,
                toolbar: options.toolbar, content: options.content,
                modal: true, cache: false, collapsible: true,
                maximizable: options.maximizable,  //允许最大化
                buttons: buttons, iconCls: options.iconCls,
                onLoad: function () {
                    options.onLoad(); $.AdjustmentDialog(dialog);
                },
                onClose: function () { $(this).dialog("destroy"); }
            });
            return dialog;
        },

        //实体操作（引入现有用户）
        MutualImport: function (title, href) {
            $.ModelDialog({
                title: title, href: href, width: 540,
                handler: function () {
                    //var userRow = $("#userDataGrid").datagrid("getSelected");
                    //if (userRow) {
                    //    $.EasyuiSlide(row.ID);
                    //}
                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#form-importUser",
                        url: "/Account/ImportUserPost",
                        success: function (result) {
                            $.EasyuiSlide("引入用户：" + $("#hSelectedUserName").val());

                            //关闭原有的窗体
                            var btnClose = $("#btnClose");
                            $.ButtonCloseDialog(btnClose);

                            //重新弹出修改窗体，将用户信息加载到窗体中，然后重新传
                            var orgRow = $("#myTreeGrid").treegrid("getSelected");
                            //如果选中了机构，则将机构也传给编辑窗体
                            if (orgRow) {
                                Account.Mutual("修改", "/Account/Edit?ID=" + $("#hSelectedUserID").val() + "&newOrgID=" + orgRow["ID"]);
                            }
                            else {
                                Account.Mutual("修改", "/Account/Edit?ID=" + $("#hSelectedUserID").val());
                            }
                        }
                    });
                },
                onLoad: function () {
                    //Account.FormInit();
                }
            });
        },

        //修改
        Edit: function () {
            var row = $("#myDataGrid").datagrid("getSelections");
            if (row.length == 1) {
                Account.Mutual("修改", "/Account/Edit?ID=" + row[0].ID + "&oldOrgID=" + row[0].OrgID);
            }
            else { $.EasyuiSlide("请选择一条记录"); }
        },

        //实体操作
        Mutual: function (title, href) {
            $.ModelDialog({
                title: title, href: href, width: 360,
                handler: function () {
                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#form-model",
                        url: "/Account/Edit",
                        success: function (result) { Account.Reload(); }
                    });
                },
                onLoad: function () {
                    Account.FormInit();
                }
            });
        },

        //弹窗初始化
        FormInit: function () {
            //定义父级的机构树
            $("#OrgID").combotree({
                width: 150, url: "/OrgInfo/GetOrgComboTree"
                //onLoadSuccess: function (node, data) {
                //    var nodeid = $("#ID").val();
                //    if (nodeid) {
                //        var etree = $("#OrgID").combotree("tree");
                //        etree.tree("remove", etree.tree('find', nodeid).target);
                //    }
                //}
            });
            //定义机构类型枚举下拉
            $("#UserTypeNum").combobox({
                editable: false, width: 150, panelHeight: "auto",
                data: [
                    { value: "0", text: "超级管理员" },
                    //{ value: "1", text: "管理员" },
                    { value: "2", text: "普通用户" }
                ]
            });
        },

        //重置密码
        ResetPassword: function () {
            var row = $("#myDataGrid").datagrid("getSelected");
            if (row) {
                $.messager.confirm('操作提示', "确认重置[ " + row.UserName + " ]的密码吗？", function (r) {
                    if (r) {
                        $.post("/Account/ResetPassword?ID=" + row.ID, function (result) {
                            if (result.ResultType == 0) {  //0表示成功
                                //移除删除的数据
                                $.EasyuiSlide(result.Message);
                            }
                            else {
                                $.EasyuiMsg(result.Message);
                            }
                        });
                    }
                });
            }
            else {
                $.EasyuiSlide("请选择一条记录");
            }
        },

        //修改密码
        //ChangePassword: function () {
        //    var row = $("#myDataGrid").datagrid("getSelected");
        //    if (row) {
        //        $.ModelDialog({
        //            title: "修改密码", href: "/Account/ChangePassword?userID=" + row.ID, width: 320,
        //            handler: function () {
        //                $.ModelForm({
        //                    validate: true, button: $(this),
        //                    id: "#form-changePassword",
        //                    url: "/Account/ChangePasswordPost?userID=" + row.ID,
        //                    success: function (result) {
        //                    }
        //                });
        //            },
        //            onLoad: function () {
        //            }
        //        });
        //    }
        //    else {
        //        $.EasyuiSlide("请选择一条记录");
        //    }
        //},

        //删除
        Delete: function () {
            var row = $("#myDataGrid").datagrid("getSelected");
            if (row) {
                if (row.UserName == "admin") {
                    $.EasyuiMsg("帐号admin不允许删除");
                }
                else {
                    $.messager.confirm('操作提示', "确认删除[ " + row.UserName + " ]吗？", function (r) {
                        if (r) {
                            $.post("/Account/Delete?ID=" + row.ID + "&orgID=" + row.OrgID, function (result) {
                                if (result.ResultType == 0) {  //0表示成功
                                    //移除删除的数据
                                    $.EasyuiSlide(result.Message);
                                    Account.Reload();
                                }
                                else {
                                    $.EasyuiMsg(result.Message);
                                }
                            });
                        }
                    });
                }
            }
            else {
                $.EasyuiSlide("请选择一条记录");
            }
        },

        //启用
        Enable: function () {
            Account.SetEnable("True");
        },

        //禁用
        Disable: function () {
            Account.SetEnable("False");
        },

        //设置启用或禁用
        SetEnable: function (isEnable) {
            var row = $("#myDataGrid").datagrid("getSelected");
            if (row) {
                $.post("/Account/SetEnable", { id: row.ID, isEnable: isEnable }, function (result) {
                    if (result.ResultType == 0) {
                        $.EasyuiSlide(result.Message);
                        Account.Reload();
                    }
                    else {
                        $.EasyuiMsg(result.Message);
                    }
                });
            }
            else { $.EasyuiSlide("请选择一条记录"); }
        },

        //重新加载数据
        Reload: function () {
            //$("#myDataGrid").datagrid("clearSelections");  //清空所有选中项
            $("#myDataGrid").datagrid("reload");
        },

        ReloadData: function () {
            //$.EasyuiSlide("abc");
            var obj = document.getElementById("isContainsChildrenOrg");
            var isContainsChildrenOrg = obj.checked;  //取得值（是否显示下级机构）

            var selectedOrg = $("#myTreeGrid").treegrid("getSelected");  //取得选中的机构
            if (selectedOrg) {
                $('#myDataGrid').datagrid('load', { orgID: selectedOrg.ID, isContainsChildrenOrg: isContainsChildrenOrg });
            }
        },

        //用户授权
        Purview: function () {
            //$.EasyuiSlide("用户授权");
            var row = $('#myDataGrid').datagrid('getSelected');
            if (row) {
                if (row.UserOrgID == null || row.UserOrgID.length == 0) {
                    $.EasyuiMsg("用户未指定机构，无法授权");
                }
                else {
                    Account.UserPurviewMutual("用户授权", "/Purview/UserPurview?UserOrgID=" + row.UserOrgID);
                }
            } else {
                //$.messager.alert('操作提示', '请选择一条数据!', 'warning');
                $.EasyuiMsg("请选择一条数据");
            }
        },

        //用户授权操作
        UserPurviewMutual: function (title, href) {
            $.ModelDialog({
                title: title, href: href, width: 700, maximizable: true,
                handler: function () {
                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#form-orgPurview",
                        url: "/Purview/UserPurviewPost",
                        success: function (result) {
                            //OrgInfo.Reload();
                        }
                    });
                },
                onLoad: function () { OrgInfo.FormInit(); }
            });
        }
    }
</script>

@*<div class="easyui-layout" data-options="fit:true">
        <div region="west" style="width:200px; border-right:0px;">
            <div class="easyui-panel" title="机构信息" iconcls="icon-user" data-options="fit:true,border:false">
                <div class="datagrid-toolbar">
                    <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Account.ExpandTree()' data-options='plain:true,iconCls:"icon-application_add"'>展开</a>
                    <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Account.ColloseTree()' data-options='plain:true,iconCls:"icon-application_delete"'>收起</a>
                    <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Account.AllOrg()' data-options='plain:true,iconCls:"icon-application_form_magnify"'>全部</a>
                </div>
                <ul id="myTree"></ul>
            </div>
        </div>
        <div region="center" data-options="border:false">
            <div class="easyui-panel" @Html.Raw(ViewBag.PanelInfo)>
                <table id="myDataGrid" fit="true"></table>
                <div id="user-tools">@Html.Raw(ViewBag.ToolBarInfo)</div>
            </div>
        </div>
    </div>*@

<!--面板（从数据库取得菜单名称和图标等）-->
<div class="easyui-panel" @Html.Raw(ViewBag.PanelInfo)>
    <table id="myDataGrid" fit="true"></table>
    <!--数据列表（根据JS构造数据）-->
    @*<div id="myToolBar">
        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Account.Add()' data-options='plain:true,iconCls:"icon-application_add"'>新增</a>
        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Account.Edit()' data-options='plain:true,iconCls:"icon-application_delete"'>修改</a>
        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Account.ResetPassword()' data-options='plain:true,iconCls:"icon-cart"'>重置密码</a>
        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Account.Delete()' data-options='plain:true,iconCls:"icon-bell"'>删除</a>
        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Account.Disable()' data-options='plain:true,iconCls:"icon-bank"'>禁用</a>
        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Account.Enable()' data-options='plain:true,iconCls:"icon-bestseller"'>启用</a>
        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Account.Purview()' data-options='plain:true,iconCls:"icon-black_board"'>用户授权</a>
        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Account.Reload()' data-options='plain:true,iconCls:"icon-blue_lego"'>刷新</a>
        <span>
            <input id="isContainsChildrenOrg" type="checkbox" onclick="Account.ReloadData()" />显示下级机构
        </span>
    </div>*@
    <div id="myToolBar">@Html.Raw(ViewBag.ToolBarInfo)</div>
</div>
